/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: app2022 - T11
 * <p>Powered by webrx On 2022-02-07 10:23:09
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T11 {

    public static void main(String[] args) {
        var t = new TT();
        //worker add
        new Thread(() -> {
            System.out.println("工作线程开始，增加元素");
            for (int i = 0; i < 20; i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                t.add();
            }
        }, "T1").start();

        new Thread(() -> {
            System.out.println("监控线程开始.");
            while (!t.count()) ;
            System.out.println("集合元素到5个了...");
            System.out.println("监控线程线束了");
        }, "T2").start();
    }
}

class TT {
    volatile List<String> list = new ArrayList<>();

    public void add() {
        String item = "item" + list.size();
        list.add(item);
        System.out.println(item);
    }

    public boolean count() {
        return list.size() >= 5;
    }

}
